package com.longrise.co.ui.carlife.webview;

import android.content.Context;
import android.content.Intent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.longrise.co.R;
import com.ly.common.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2016/6/6.
 */
public class CommonWebViewActivity extends BaseActivity {
    @Bind(R.id.head_title)
    TextView headTitle;
    //标题
    private String title;
    //链接
    private String url;

    @Bind(R.id.wb_content)
    WebView mWebView;

    public static void gotoWebActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, CommonWebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_common_webview;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        title = intent.getStringExtra("title");
        url = intent.getStringExtra("url");
        headTitle.setText(title);

        mWebView.clearHistory();
        //扩大比例的缩放
        mWebView.getSettings().setUseWideViewPort(true);
        //设置编码　　
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        //支持javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        mWebView.getSettings().setSupportZoom(false);
        // 设置出现缩放工具
        mWebView.getSettings().setBuiltInZoomControls(false);

        //设置 缓存模式 不用缓存 使用localStorage
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String absolutePath = mContext.getApplicationContext().getCacheDir().getAbsolutePath();
        mWebView.getSettings().setAppCachePath(absolutePath);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAppCacheEnabled(true);

        //自适应屏幕
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });

        mWebView.loadUrl(url);
    }


    @OnClick(R.id.head_back)
    public void onViewClicked() {
        finish();
    }
}
